﻿using Serilog;
using System;
using System.Collections.Generic;

namespace ConsoleApp5
{
    public class ShoppingCart
    {
        private ILogger _logger;
        private List<Product> _products;
        private decimal _balance;

        public ShoppingCart()
        {
            // 配置Serilog日志记录器
            _logger = new LoggerConfiguration()
                .MinimumLevel.Debug()
                .WriteTo.Console()
                .WriteTo.File("logs/log.txt", rollingInterval: RollingInterval.Day)
                .CreateLogger();

            // 预先添加一些商品
            _products = new List<Product>
            {
                new Product { Id = "1", Name = "西瓜", Price = 5.0m },
                new Product { Id = "2", Name = "汽水", Price = 2.5m },
                new Product { Id = "3", Name = "苹果", Price = 3.0m },
                new Product { Id = "4", Name = "饼干", Price = 1.5m }
            };

            // 客户的余额预先是10块钱
            _balance = 10.0m;
        }

        public void Start()
        {
            Console.WriteLine("欢迎使用购物车应用！");
            Console.WriteLine("请输入操作：");
            Console.WriteLine("1. 添加商品到购物车");
            Console.WriteLine("2. 结账");
            Console.WriteLine("3. 充值");
            Console.WriteLine("4. 退出");

            bool exit = false;
            while (!exit)
            {
                string input = Console.ReadLine();
                switch (input)
                {
                    case "1":
                        AddItemToCart();
                        break;
                    case "2":
                        Checkout();
                        break;
                    case "3":
                        Recharge();
                        break;
                    case "4":
                        exit = true;
                        break;
                    default:
                        Console.WriteLine("无效的输入，请重新输入！");
                        break;
                }
            }
        }

        public void AddItemToCart()
        {
            Console.WriteLine("请输入商品ID：");
            string itemId = Console.ReadLine();

            Console.WriteLine("请输入商品数量：");
            int quantity;
            while (!int.TryParse(Console.ReadLine(), out quantity))
            {
                Console.WriteLine("无效的数量，请重新输入：");
            }

            // 查找商品
            Product product = _products.Find(p => p.Id == itemId);
            if (product == null)
            {
                Console.WriteLine("找不到该商品！");
                return;
            }

            // 检查库存
            if (quantity > 0)
            {
                // 将信息日志记录到文件
                _logger.Information("用户添加商品到购物车。商品ID: {ItemId}，数量: {Quantity}", itemId, quantity);

                // 添加商品到购物车的逻辑
                // ...
                Console.WriteLine("商品已成功添加到购物车！");
            }
            else
            {
                Console.WriteLine("无效的数量！");
            }
        }

        public void Checkout()
        {
            // 结账逻辑
            decimal total = CalculateTotal();
            if (total > _balance)
            {
                // 将警告日志记录到文件
                _logger.Warning("用户结账时余额不足的警告");

                Console.WriteLine("余额不足，请充值或删除部分商品！");
            }
            else
            {
                // 扣除金额
                _balance -= total;

                // 将信息日志记录到文件
                _logger.Information("用户结账。总金额: {Total}", total);

                Console.WriteLine("结账完成！");
            }
        }

        public void Recharge()
        {
            Console.WriteLine("请输入充值金额：");
            decimal amount;
            while (!decimal.TryParse(Console.ReadLine(), out amount) || amount <= 0)
            {
                Console.WriteLine("无效的金额，请重新输入：");
            }

            // 充值
            _balance += amount;

            // 将信息日志记录到文件
            _logger.Information("用户充值。金额: {Amount}", amount);

            Console.WriteLine("充值成功！当前余额为：" + _balance);
        }

        private decimal CalculateTotal()
        {
            // 计算购物车中商品的总金额
            decimal total = 0.0m;
            // ...
            return total;
        }
    }

    public class Product
    {
        public string Id { get; set; }
        public string Name { get; set; }
        public decimal Price { get; set; }
    }

    public class Program
    {
        public static void Main(string[] args)
        {
            var shoppingCart = new ShoppingCart();
            shoppingCart.Start();
        }
    }
}
